<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    /** @use HasFactory<\Database\Factories\UserFactory> */
    use HasFactory, Notifiable;

    /**
     * 使用整数时间戳
     */
    protected $dateFormat = 'U';

    const STATUS_ON  = 0;
    const STATUS_OFF = 1;

    const STATUS_MAPPING = [
        self::STATUS_ON  => '正常',
        self::STATUS_OFF => '异常'
    ];

    const GENDER_DEFAULT = 0;
    const GENDER_MAN = 1;
    const GENDER_WOMAN = 2;
    const GENDER_MAP = [
        self::GENDER_DEFAULT => '未设置',
        self::GENDER_MAN     => '男',
        self::GENDER_WOMAN   => '女'
    ];

    /**
     * 时间戳字段为整数类型
     */
    protected $casts = [
        'created_at'        => 'integer',
        'updated_at'        => 'integer',
        'email_verified_at' => 'datetime',
        'password'          => 'hashed',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'name',
        'phone',
        'email',
        'password',
        'avatar',
        'target_weight',
        'height',
        'birthday',
        'gender',
    ];
    
    protected $appends = [
        'gender_text'
    ];


    /**
     * The attributes that should be hidden for serialization.
     *
     * @var list<string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * 格式化时间为东八区显示
     *
     * @param mixed $value
     * @param string $format
     * @return string
     */
    protected function formatDateToTimezone($value, $format = 'Y-m-d H:i')
    {
        if (!$value) {
            return null;
        }

        return $this->asDateTime($value)
            ->setTimezone('Asia/Shanghai')
            ->format($format);
    }

    /**
     * 获取创建时间（东八区）
     */
    public function getCreatedAtAttribute($value)
    {
        return $this->formatDateToTimezone($value);
    }

    /**
     * 获取更新时间（东八区）
     */
    public function getUpdatedAtAttribute($value)
    {
        return $this->formatDateToTimezone($value);
    }

    /**
     * 获取性别文本
     */
    public function getGenderTextAttribute()
    {
        return self::GENDER_MAP[$this->gender] ?? self::GENDER_MAP[self::GENDER_DEFAULT];
    }


    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}
